#include "BrokerKeyInfo.h"

#include <qbprotocol/proto/table/common_util.h>

BrokerKeyInfo::BrokerKeyInfo(const BrokerKeyInfo& bki)
    : m_nIndex(bki.m_nIndex), m_nBrokerId(bki.m_nBrokerId) {}
BrokerKeyInfo::BrokerKeyInfo(const BrokerKey& key)
    : m_nIndex(key.m_nIndex), m_nBrokerId(atoi(key.GetBrokerId().c_str())) {}

BrokerKeyInfo::BrokerKeyInfo() : m_nIndex(0), m_nBrokerId(0) {}
BrokerKeyInfo::BrokerKeyInfo(const char* szid, int nIndex)
    : m_nIndex(nIndex), m_nBrokerId(szid ? atoi(szid) : 0) {}
BrokerKeyInfo::BrokerKeyInfo(const std::string& szid, int nIndex)
    : m_nIndex(nIndex), m_nBrokerId(atoi(szid.c_str())) {}
BrokerKeyInfo& BrokerKeyInfo::operator=(const BrokerKeyInfo& bki) {
  m_nIndex = bki.m_nIndex, m_nBrokerId = bki.m_nBrokerId;
  return *this;
}
unsigned int BrokerKeyInfo::GetHash() const {
  return (m_nBrokerId << 24) | m_nIndex;
  // return ss::base::GetFastHash(this,sizeof(this),0);
}
bool BrokerKeyInfo::operator==(const BrokerKeyInfo& key) const {
  return m_nIndex == key.m_nIndex && m_nBrokerId == key.m_nBrokerId;
}
const bool BrokerKeyInfo::operator<(const BrokerKeyInfo& obj) const {
  if (m_nBrokerId < obj.m_nBrokerId)
    return true;
  else if (m_nBrokerId == obj.m_nBrokerId && m_nIndex < obj.m_nIndex) {
    return true;
  } else
    return false;
}
void BrokerKeyInfo::GetBrokerKey(BrokerKey& bkey) const {
  char buf[32] = {0};
  qb::proto::MathUtil::itoa(m_nBrokerId, buf, _countof(buf));
  bkey.m_nIndex = m_nIndex;
  bkey.SetBrokerId(buf);
}